package drds.binlog.parse;

import drds.binlog.binlog_event.event.TableMapLogEvent;
import drds.binlog.common.Authors;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述数据meta对象,mysql binlog中对应的{@linkplain TableMapLogEvent}包含的信息不全
 * <pre>
 * 1. 主键信息
 * 2. column name
 * 3. unsigned字段
 * </pre>
 */
@Author(name = Authors.LI_YANG)
public class TableMetaData
{

    @Setter
    @Getter
    private String databaseName;
    @Setter
    @Getter
    private String tableName;
    @Setter
    @Getter
    private List<FieldMetaData> fieldMetaDataList = new ArrayList<FieldMetaData>();
    @Setter
    @Getter
    private String ddl;                                          // 表结构的DDL语句

    public TableMetaData()
    {

    }

    public TableMetaData(String databaseName, String tableName, List<FieldMetaData> fieldMetaDataList)
    {
        this.databaseName = databaseName;
        this.tableName = tableName;
        this.fieldMetaDataList = fieldMetaDataList;
    }

    public String getFullName()
    {
        return databaseName + "." + tableName;
    }


    public FieldMetaData getFieldMetaData(String columnName)
    {
        for (FieldMetaData fieldMetaData : fieldMetaDataList)
        {
            if (fieldMetaData.getColumnName().equalsIgnoreCase(columnName))
            {
                return fieldMetaData;
            }
        }

        throw new RuntimeException("unknow column : " + columnName);
    }

    public List<FieldMetaData> getPrimaryKeyFieldMetaDataList()
    {
        List<FieldMetaData> fieldMetaDataList = new ArrayList<FieldMetaData>();
        for (FieldMetaData fieldMetaData : this.fieldMetaDataList)
        {
            if (fieldMetaData.isPrimaryKey())
            {
                fieldMetaDataList.add(fieldMetaData);
            }
        }

        return fieldMetaDataList;
    }


}
